home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Online / NNTPd / server / date.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-11-01  |  4.6 KB  |  243 lines

  1. #ifndef lint
  2. static char    sccsid[] = "@(#)$Id: date.c,v 1.2 1994/11/01 06:08:21 sob Exp sob $";
  3. #endif
  4.  
  5. /*
  6.  * Collection of routines for dealing with ASCII time strings.
  7.  * These may actually be useful in their own right.
  8.  */
  9.  
  10. #include "common.h"
  11. #ifdef USG
  12. #include <time.h>
  13. #else
  14. #include <sys/time.h>
  15. #endif
  16.  
  17. dodate(ac, av)
  18.     int    ac;
  19.     char    *av[];
  20. {
  21.     struct tm    *gmt;
  22. #ifdef USG
  23.     time_t        now;
  24.  
  25.     (void) time(&now);
  26.     gmt = gmtime(&now);
  27. #else /* not USG */
  28.     struct timeval    now;
  29.  
  30.     (void) gettimeofday(&now, (struct timezone *)NULL);
  31.     gmt = gmtime(&now.tv_sec);
  32. #endif /* not USG */
  33.     printf("%d %04.4d%02.2d%02.2d%02.2d%02.2d%02.2d\r\n", INF_DATE,
  34.         gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
  35.         gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
  36.     (void) fflush(stdout);
  37. }
  38.  
  39. /*
  40.  * dtol -- convert date to long integer.  This is not implicitly
  41.  * local time, or any other kind of time, for that matter.  If you
  42.  * pass it a date you think is GMT, you wind up with that number of
  43.  * seconds...
  44.  *
  45.  *    Parameters:        "date_ascii" is in the form "yymmddhhmmss".
  46.  *
  47.  *    Returns:        Long integer containing number
  48.  *                of seconds since 000000 Jan 1, 1970,
  49.  *                and "date".  -1 on error.
  50.  *
  51.  *    Side effects:        None.
  52.  */
  53.  
  54. #define twodigtoi(x)    (((x[0]) - '0') + (x[1] - '0')*10)
  55. #define    dysize(y)    ((y % 4 ? 365 : 366))
  56.  
  57. static    int    dmsize[12] =
  58.     { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  59.  
  60. long
  61. dtol(date_ascii)
  62.     char    *date_ascii;
  63. {
  64.     char    date[32], *date_str;
  65.     char    *lhs, *rhs;
  66.     char    temp;
  67.     long    seconds;
  68.     int    year, month, day, hour, mins, secs;
  69.     int    len, i;
  70.  
  71.     len = strlen(date_ascii);
  72.     if (len != sizeof("yymmddhhmmss")-1)
  73.         return (-1);
  74.  
  75.     (void) strcpy(date, date_ascii);
  76.     date_str = date;
  77.  
  78. #ifdef DEBUG
  79.     if (debug > 1)
  80.         syslog(LOG_DEBUG, "(1) date_str = \"%s\"", date_str);
  81. #endif
  82.     rhs = date_str + len - 1;
  83.     lhs = date_str;
  84.  
  85.     for (; lhs < rhs; ++lhs, --rhs) {
  86.         temp = *lhs;
  87.         *lhs = *rhs;
  88.         *rhs = temp;
  89.         if (!isdigit(temp) || !isdigit(*lhs))
  90.             return (-1);
  91.     }
  92.  
  93.     lhs = date_str;
  94. #ifdef DEBUG
  95.     if (debug > 1)
  96.         syslog(LOG_DEBUG, "(2) date_str = \"%s\"", date_str);
  97. #endif
  98.  
  99.     secs = twodigtoi(lhs);
  100.     lhs += 2;
  101.     mins = twodigtoi(lhs);
  102.     lhs += 2;
  103.     hour = twodigtoi(lhs);
  104.     lhs += 2;
  105.     day = twodigtoi(lhs);
  106.     lhs += 2;
  107.     month = twodigtoi(lhs);
  108.     lhs += 2;
  109.     year = twodigtoi(lhs);
  110.  
  111.     if (month < 1 || month > 12 ||
  112.         day < 1 || day > 31 ||
  113.         mins < 0 || mins > 59 ||
  114.         secs < 0 || secs > 59)
  115.         return (-1);
  116.     if (hour == 24) {
  117.         hour = 0;
  118.         day++;
  119.     }
  120.     if (hour < 0 || hour > 23)
  121.         return (-1);
  122.     seconds = 0;
  123.     year += 1900;
  124.     for (i = 1970; i < year; i++)
  125.         seconds += dysize(i);
  126.     /* Leap year */
  127.     if (dysize(year) == 366 && month >= 3)
  128.         seconds++;
  129.     while (--month)
  130.         seconds += dmsize[month-1];
  131.     seconds += day-1;
  132.     seconds = 24 * seconds + hour;
  133.     seconds = 60 * seconds + mins;
  134.     seconds = 60 * seconds + secs;
  135.  
  136.     return (seconds);
  137. }
  138.  
  139.  
  140. /*
  141.  * ltod -- convert long integer to date string.
  142.  *
  143.  *    Parameters:        "date" is in the number of seconds
  144.  *                since the epoch.
  145.  *
  146.  *    Returns:        Pointer to static data in the form
  147.  *                yymmddhhmmss\0.
  148.  *
  149.  *    Side effects:        None.
  150.  */
  151.  
  152. char *
  153. ltod(date)
  154.     long        date;
  155. {
  156.     static char    timebuf[32];
  157.     struct tm    *tp;
  158.  
  159.     tp = gmtime(&date);
  160.  
  161.     (void) sprintf(timebuf, "%02d%02d%02d%02d%02d%02d",
  162.         tp->tm_year,
  163.         tp->tm_mon + 1,        /* 0 to 11??? How silly. */
  164.         tp->tm_mday,
  165.         tp->tm_hour,
  166.         tp->tm_min,
  167.         tp->tm_sec);
  168.  
  169.     return (timebuf);
  170. }
  171.  
  172.  
  173. /*
  174.  * local_to_gmt -- convert a local time (in form of number of
  175.  * seconds since you-know-when) to GMT.
  176.  *
  177.  *    Parameters:    "date" is date we want converted, in
  178.  *            seconds since 000000 1 Jan 1970.
  179.  *
  180.  *    Returns:    Number of seconds corrected for local
  181.  *            and dst.
  182.  */
  183.  
  184. long
  185. local_to_gmt(date)
  186.     long    date;
  187. {
  188. #ifdef USG
  189. #if !defined(dgux) && !defined(M_XENIX)
  190.     extern    long    timezone;
  191. #endif
  192.     tzset();
  193.     date += timezone;
  194. #else /* not USG */
  195.     struct    timeval    tv;
  196.     struct    timezone tz;
  197.  
  198.     (void) gettimeofday(&tv, &tz);
  199.     date += (long) tz.tz_minuteswest * 60;
  200. #endif /* not USG */
  201.  
  202.     /* now fix up local daylight time */
  203.     if (localtime((time_t *)&date)->tm_isdst)
  204.         date -= 60*60;
  205.  
  206.     return (date);
  207. }
  208.  
  209. /*
  210.  * gmt_to_local -- take a GMT time expressed in seconds since
  211.  * the epoch, and convert it to local time.
  212.  *
  213.  *    Parameters:    "date" is the number of seconds...
  214.  *
  215.  *    Returns:    Number of seconds corrected to reflect
  216.  *            local time and dst.
  217.  */
  218.  
  219. long
  220. gmt_to_local(date)
  221.     long    date;
  222. {
  223. #ifdef USG
  224. #if !defined(dgux) && !defined(M_XENIX)
  225.     extern    long    timezone;
  226. #endif
  227.     tzset();
  228.     date -= timezone;
  229. #else /* not USG */
  230.     struct    timeval    tv;
  231.     struct    timezone tz;
  232.  
  233.     (void) gettimeofday(&tv, &tz);
  234.     date -= (long) tz.tz_minuteswest * 60;
  235. #endif /* not USG */
  236.  
  237.     /* now fix up local daylight time */
  238.     if (localtime((time_t *)&date)->tm_isdst)
  239.         date += 60*60;
  240.  
  241.     return (date);
  242. }
  243.